package tank

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"server/config"
	db "server/data"
	"server/utils"
	"strconv"
)

type GamesReq struct {
	Games []db.Game `json:"games" binding:"required,dive"`
}

func AddGames(c *gin.Context) {
	var gamesReq GamesReq
	if err := c.ShouldBindJSON(&gamesReq); err != nil {
		c.JSON(http.StatusOK, gin.H{"status": http.StatusBadRequest, "desc": err.Error()})
		return
	}

	c.Set(config.RequestLogParamKey, utils.Struct2str(gamesReq))

	if r := db.DB.Debug().Model(db.Game{}).Where("name=?", gamesReq.Games[0].Name).Find(&struct{}{}); r.RowsAffected != 0 {
		c.JSON(http.StatusOK, gin.H{"status": http.StatusBadRequest,
			"desc": "已经存在该赛事了",
		})
		return
	}

	db.DB.Debug().Create(&gamesReq.Games)
	c.JSON(http.StatusOK, gin.H{
		"status": config.BusinessSuccessCode,
	})
}

func AddGame(c *gin.Context) {
	var game db.Game
	if err := c.ShouldBindJSON(&game); err != nil {
		c.JSON(http.StatusOK, gin.H{"status": http.StatusBadRequest, "desc": err.Error()})
		return
	}

	c.Set(config.RequestLogParamKey, utils.Struct2str(game))
	db.DB.Debug().Create(&game)
	c.JSON(http.StatusOK, gin.H{
		"status": config.BusinessSuccessCode,
	})
}

type GameReq struct {
	Name string `form:"name" binding:"required"`
}

func GetGame(c *gin.Context) {
	var gameReq GameReq
	if err := c.ShouldBindQuery(&gameReq); err != nil {
		c.JSON(http.StatusOK, gin.H{"status": http.StatusBadRequest, "desc": err.Error()})
		return
	}

	var games []db.Game
	db.DB.Debug().Model(db.Game{}).Where("name=?", gameReq.Name).Find(&games)

	c.JSON(http.StatusOK, gin.H{"status": config.BusinessSuccessCode,
		"games": games,
	})
	return
}

type GameRecordReq struct {
	BattleId        int    `json:"battleId" binding:"gte=0"`
	Type            int    `json:"type" binding:"oneof=1 2 3 4"`
	AIRank          int    `json:"aiRank" binding:"oneof=0 1"`
	Debug           int    `json:"debug" binding:"oneof=0 1"`
	ECount          int    `json:"eCount" binding:"gte=0"`
	GroupName       string `json:"groupName" binding:"required"`
	PlayerA         string `json:"playerA" binding:"required"`
	PlayerAFire     int    `json:"playerAFire" binding:"gte=0"`
	PlayerAHit      int    `json:"playerAHit" binding:"gte=0"`
	PlayerAKill     int    `json:"playerAKill" binding:"gte=0"`
	PlayerALife     int    `json:"playerALife" binding:"gte=0"`
	PlayerACostTime int    `json:"playerACostTime" binding:"gte=0"`
	PlayerB         string `json:"playerB" binding:"required"`
	PlayerBFire     int    `json:"playerBFire" binding:"gte=0"`
	PlayerBHit      int    `json:"playerBHit" binding:"gte=0"`
	PlayerBKill     int    `json:"playerBKill" binding:"gte=0"`
	PlayerBLife     int    `json:"playerBLife" binding:"gte=0"`
	PlayerBCostTime int    `json:"playerBCostTime" binding:"gte=0"`
	Win             string `json:"win"`
}

func GameRecord(c *gin.Context) {
	var gameRecordReq GameRecordReq
	if err := c.ShouldBindJSON(&gameRecordReq); err != nil {
		c.JSON(http.StatusOK, gin.H{"status": http.StatusBadRequest, "desc": err.Error()})
		return
	}

	r := db.DB.Debug().Model(db.GameRecord{}).Where("battle_id=? and type=? and group_name=?", gameRecordReq.BattleId, gameRecordReq.Type, gameRecordReq.GroupName).Find(&struct{}{})
	if r.RowsAffected != 0 {
		c.JSON(http.StatusOK, gin.H{
			"status": http.StatusBadRequest,
			"desc":   "当前已经有一场id为" + strconv.Itoa(gameRecordReq.BattleId) + "且有效的比赛了，请重试！",
		})
		return
	}

	playerAGameRec := db.GameRecord{
		GroupName: gameRecordReq.GroupName,
		BattleId:  gameRecordReq.BattleId,
		Type:      gameRecordReq.Type,
		AIRank:    gameRecordReq.AIRank,
		Player:    gameRecordReq.PlayerA,
		Fire:      gameRecordReq.PlayerAFire,
		Hit:       gameRecordReq.PlayerAHit,
		Kill:      gameRecordReq.PlayerAKill,
		Life:      gameRecordReq.PlayerALife,
		CostTime:  gameRecordReq.PlayerACostTime,
	}

	playerBGameRec := db.GameRecord{
		GroupName: gameRecordReq.GroupName,
		BattleId:  gameRecordReq.BattleId,
		Type:      gameRecordReq.Type,
		AIRank:    gameRecordReq.AIRank,
		Player:    gameRecordReq.PlayerB,
		Fire:      gameRecordReq.PlayerBFire,
		Hit:       gameRecordReq.PlayerBHit,
		Kill:      gameRecordReq.PlayerBKill,
		Life:      gameRecordReq.PlayerBLife,
		CostTime:  gameRecordReq.PlayerBCostTime,
	}

	if gameRecordReq.Win == gameRecordReq.PlayerA {
		playerAGameRec.Win = 1
	} else if gameRecordReq.Win == gameRecordReq.PlayerB {
		playerBGameRec.Win = 1
	} else {
		c.JSON(http.StatusOK, gin.H{"status": http.StatusBadRequest, "desc": "win字段传值有误，请检查！"})
		return
	}

	db.DB.Debug().Create(&playerAGameRec)
	db.DB.Debug().Create(&playerBGameRec)
	db.DB.Debug().Model(&db.Game{}).Where("battle_id=? and name=?", gameRecordReq.BattleId, gameRecordReq.GroupName).Update("game_over", db.GameOverY)

	c.JSON(http.StatusOK, gin.H{"status": config.BusinessSuccessCode})
	return
}

type GameExistReq struct {
	GroupName string `form:"groupName" binding:"required"`
	BattleId  int    `form:"battleId" binding:"gte=0"`
	Type      int    `form:"type" binding:"required"`
}

func GameExist(c *gin.Context) {
	var gameExistReq GameExistReq
	if err := c.ShouldBindQuery(&gameExistReq); err != nil {
		c.JSON(http.StatusOK, gin.H{"status": http.StatusBadRequest, "desc": err.Error()})
		return
	}

	r := db.DB.Debug().Model(db.GameRecord{}).Where("battle_id=? and type=? and group_name=?", gameExistReq.BattleId, gameExistReq.Type, gameExistReq.GroupName).Find(&struct{}{})
	c.JSON(http.StatusOK, gin.H{
		"status":  config.BusinessSuccessCode,
		"isExist": r.RowsAffected != 0,
	})
}

type GameRankReq struct {
	GroupName string `form:"groupName" binding:"required"`
}

type WheelRankReq struct {
	GroupName string `form:"groupName" binding:"required"`
	Type      int    `form:"type" binding:"oneof=2 3"`
}

func WheelWarRanks(c *gin.Context) {
	var req WheelRankReq
	if err := c.ShouldBindQuery(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{"status": http.StatusBadRequest, "desc": err.Error()})
		return
	}

	result := db.QWheelWarRanks(req.GroupName, req.Type)

	for i := range result {
		result[i].Rank = i + 1
	}

	c.JSON(http.StatusOK, gin.H{"status": config.BusinessSuccessCode,
		"ranks": result,
	})
}

func AIRanks(c *gin.Context) {
	var gameRankReq GameRankReq
	if err := c.ShouldBindQuery(&gameRankReq); err != nil {
		c.JSON(http.StatusOK, gin.H{"status": http.StatusBadRequest, "desc": err.Error()})
		return
	}

	result := db.QAIRanks(gameRankReq.GroupName)

	for i := range result {
		result[i].Rank = i + 1
	}

	c.JSON(http.StatusOK, gin.H{"status": config.BusinessSuccessCode,
		"ranks": result,
	})
}

func FinalRanks2021(c *gin.Context) {
	var gameRankReq GameRankReq
	if err := c.ShouldBindQuery(&gameRankReq); err != nil {
		c.JSON(http.StatusOK, gin.H{"status": http.StatusBadRequest, "desc": err.Error()})
		return
	}

	result := db.QFinalRanks(gameRankReq.GroupName)

	for i := range result {
		result[i].Rank = i + 1
	}

	c.JSON(http.StatusOK, gin.H{"status": config.BusinessSuccessCode,
		"ranks": result,
	})
}
